<?php

/**
 * weaveOrderItem actions.
 *
 * @package    WacStorehouse
 * @subpackage weaveOrderItem
 * @author     JianBinBi <prince.bi@gmail.com>
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 *
 * notes:
 * if clone as another one, below tags need to be replace to ur target module tag
 * "weaveOrder" , "WeaveOrder", "weave_order"
 */
class weaveOrderItemActions extends WacCommonActions
{
    /**
     * get searching list
     * @package
     * @author     ben
     */
    public function executeGetList(sfWebRequest $request) {

        $jqGridDataHelper = JqGridDataHelper::getInstance();

        $arrParam = array();
        if($jqGridDataHelper->loadComplexQuery($request)) {

            if($request->hasParameter(JqGridDataHelper::$KEY_SEARCH_FIELD) && $request->getParameter(JqGridDataHelper::$KEY_SEARCH_OPER)!="") {
                $arrParam['andWhere'] = array(
                        $jqGridDataHelper->getCondMapStr($request->getParameter(JqGridDataHelper::$KEY_SEARCH_OPER), "t1.".$request->getParameter(JqGridDataHelper::$KEY_SEARCH_FIELD), $request->getParameter(JqGridDataHelper::$KEY_SEARCH_STRING))
                );
            }

            if($request->hasParameter(JqGridDataHelper::$KEY_SIDX)) {
                switch($request->getParameter(JqGridDataHelper::$KEY_SIDX)) {
                    case "weave_order_code":
                        $arrParam['orderBy'] = "wwo.code ".$request->getParameter(JqGridDataHelper::$KEY_SORD);
                        break;
//                    case "jar_name":
//                        $arrParam['orderBy'] = "wj.name ".$request->getParameter(JqGridDataHelper::$KEY_SORD);
//                        break;
                    case "factory_name":
                        $arrParam['orderBy'] = "wf.name ".$request->getParameter(JqGridDataHelper::$KEY_SORD);
                        break;
//                    case "goods_category_name":
//                        $arrParam['orderBy'] = "wgc.name ".$request->getParameter(JqGridDataHelper::$KEY_SORD);
//                        break;
                    case "goods_name":
                        $arrParam['orderBy'] = "wg.name ".$request->getParameter(JqGridDataHelper::$KEY_SORD);
                        break;
//                    case "cotton_yarn_unit_consume":
//                        $arrParam['orderBy'] = "wwoicc.cotton_yarn_unit_consume ".$request->getParameter(JqGridDataHelper::$KEY_SORD);
//                        break;
//                    case "cotton_yarn_expect_consume":
//                        $arrParam['orderBy'] = "wwoicc.cotton_yarn_expect_consume ".$request->getParameter(JqGridDataHelper::$KEY_SORD);
//                        break;
//                    case "attrition_rate":
//                        $arrParam['orderBy'] = "wwoicc.attrition_rate ".$request->getParameter(JqGridDataHelper::$KEY_SORD);
//                        break;
                    default:
                        $arrParam['orderBy'] = "t1.".$request->getParameter(JqGridDataHelper::$KEY_SIDX)." ".$request->getParameter(JqGridDataHelper::$KEY_SORD);
                        break;
                }
            }

            $resultSet = $this->mainModuleTable->getCustomList($arrParam, $request->getParameter(JqGridDataHelper::$KEY_PAGE), $request->getParameter(JqGridDataHelper::$KEY_ROWS), false);
        }
        else {
            if($request->hasParameter(JqGridDataHelper::$KEY_SEARCH_FIELD) && $request->getParameter(JqGridDataHelper::$KEY_SEARCH_FIELD)=="parent_id") {
                $arrParam['andWhere'][] = "weave_order_id=".$this->getRequest()->getParameter(JqGridDataHelper::$KEY_SEARCH_STRING);
            }
            $resultSet = $this->mainModuleTable->getCommonList($request->getParameter(JqGridDataHelper::$KEY_PAGE), $request->getParameter(JqGridDataHelper::$KEY_ROWS), false, $arrParam);
        }

        $filterResultSet = $this->filterList($resultSet);

        $pager = $this->mainModuleTable->getPager();
        $filterResultSet = $jqGridDataHelper->convert($filterResultSet, $pager, JsCommonData::getSuccessDatum());

        return OutputHelper::getInstance()->outputJsonOrTextFormat($filterResultSet, $this);
    }

    /*
   * override filter list
    */
    public function filterList($listObjs) {
        $filterArr = array();
        if(count($listObjs) > 0) {
            foreach($listObjs as $listObj) {
                $tmpArr = $listObj->toArray();
                $tmpArr['production_order_code']        = $listObj->getWacProductionOrder()->getCode();
                $tmpArr['weave_order_code']             = $listObj->getWacWeaveOrder()->getCode();
                $tmpArr['goods_number_unit_code_name']  = $listObj->getWacGoodsNumberUnitCode()->getName();
                $tmpArr['factory_name']                 = $listObj->getWacFactory()->getName();
                $tmpArr['color_name']                   = $listObj->getWacColor()->getName();
//                $tmpArr['jar_name']                     = $listObj->getWacJar()->getName();
                $tmpArr['spinner_name']                 = $listObj->getWacSpinner()->getName();
                $tmpArr['filling_yarn_arrangement_name']= $listObj->getWacFillingYarnArrangement()->getName();
                $tmpArr['warp_yarn_arrangement_name']     = $listObj->getWacWarpArrangement()->getName();
                $tmpArr['spinner_name']                 = $listObj->getWacSpinner()->getName();
                $tmpArr['goods_category_name']          = $listObj->getWacGoodsCategory()->getName();
                $tmpArr['goods_name']                   = $listObj->getWacGoods()->getName();

//                $cottonItemConsume = Doctrine::getTable(WacTable::$weaveOrderItemCottonConsume)->getListByItem($listObj)->getFirst();
//                $tmpArr['cotton_goods_id']     = $cottonItemConsume->getGoodsId();
//                $tmpArr['cotton_goods_name']   = $cottonItemConsume->getWacGoods()->getName();
//                $tmpArr['cotton_yarn_consume_amount']   = $cottonItemConsume->getCottonYarnConsumeAmount();
//                $tmpArr['cotton_yarn_unit_consume']     = $cottonItemConsume->getCottonYarnUnitConsume();
//                $tmpArr['cotton_yarn_expect_consume']   = $cottonItemConsume->getCottonYarnExpectConsume();
//                $tmpArr['cotton_yarn_attrition_rate']   = $cottonItemConsume->getAttritionRate();
//
//                $warpYarnItemConsume = Doctrine::getTable(WacTable::$weaveOrderItemWarpYarnConsume)->getListByItem($listObj)->getFirst();
//                $tmpArr['warp_yarn_goods_id']  = $warpYarnItemConsume->getGoodsId();
//                $tmpArr['warp_yarn_goods_name']= $warpYarnItemConsume->getWacGoods()->getName();
//                $tmpArr['warp_yarn_consume_amount']     = $warpYarnItemConsume->getCottonYarnConsumeAmount();
//                $tmpArr['warp_yarn_unit_consume']       = $warpYarnItemConsume->getCottonYarnUnitConsume();
//                $tmpArr['warp_yarn_expect_consume']     = $warpYarnItemConsume->getCottonYarnExpectConsume();
//                $tmpArr['warp_yarn_attrition_rate']     = $warpYarnItemConsume->getAttritionRate();

                $filterArr[] = $tmpArr;
            }
        }

        return $filterArr;
    }

    /*
   * @return inspect result
    */
    public function inspectDataValidation(sfWebRequest $request, $params=array()) {
        $result    = JsCommonData::getSuccessDatum();
        $reqParams = $request->getParameterHolder()->getAll();

//        if(empty($reqParams['goods_id']) || !Doctrine::getTable(WacTable::$goodsCategory)->isExistedId($reqParams['goods_id'])) {
//            $result = JsCommonData::getErrorDatum(
//                    Doctrine::getTable(WacTable::$sysmsg)->getContentByCode("sys_err_invalid_goods_category"),  WacErrorCode::$logicError
//            );
//            return $result;
//        }

        if(empty($reqParams['weave_order_id']) || !Doctrine::getTable(WacTable::$weaveOrder)->isExistedId($reqParams['weave_order_id'])) {
            $result = JsCommonData::getErrorDatum(
                    Doctrine::getTable(WacTable::$sysmsg)->getContentByCode("sys_err_invalid_weave_order"),  WacErrorCode::$logicError
            );
            return $result;
        }

        if(empty($reqParams['color_id']) || !Doctrine::getTable(WacTable::$color)->isExistedId($reqParams['color_id'])) {
            $result = JsCommonData::getErrorDatum(
                    Doctrine::getTable(WacTable::$sysmsg)->getContentByCode("sys_err_invalid_color"),  WacErrorCode::$logicError
            );
            return $result;
        }

//        if(empty($reqParams['jar_id']) || !Doctrine::getTable(WacTable::$jar)->isExistedId($reqParams['jar_id'])) {
//            $result = JsCommonData::getErrorDatum(
//                    Doctrine::getTable(WacTable::$sysmsg)->getContentByCode("sys_err_invalid_jar"),  WacErrorCode::$logicError
//            );
//            return $result;
//        }

        if(empty($reqParams['spinner_id']) || !Doctrine::getTable(WacTable::$spinner)->isExistedId($reqParams['spinner_id'])) {
            $result = JsCommonData::getErrorDatum(
                    Doctrine::getTable(WacTable::$sysmsg)->getContentByCode("sys_err_invalid_spinner"),  WacErrorCode::$logicError
            );
            return $result;
        }

        return $result;
    }

    /*
   * @return list data array
    */
    public function executeAdd(sfWebRequest $request) {
        if ($this->getRequest()->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $reqParams     = $this->getRequest()->getParameterHolder()->getAll();
        $resultSet     = JsCommonData::getCommonDatum();
        $inspectResult = $this->inspectDataValidation($request);

        if($inspectResult['status'] == WacOperationStatus::$Error) {
            $resultSet['info'] = $inspectResult;
        }
        else {
            $objWeaveOrder = Doctrine::getTable(WacTable::$weaveOrder)->findOneBy('id', $reqParams['weave_order_id']);
            $targetItem      = $this->mainModuleTable->create();

            if(count($reqParams)>0) {
//              $targetItem->set($reqParams['']);

                $targetItem->setProductionOrderId($objWeaveOrder->getProductionOrderId());
                $targetItem->setSrcProductionOrderId($objWeaveOrder->getProductionOrderId());
                $targetItem->setWeaveOrderId($objWeaveOrder->getId());
                $targetItem->setSupplierId($objWeaveOrder->getSupplierId());
                $targetItem->setFactoryId($objWeaveOrder->getFactoryId());

                if(isset($reqParams['goods_id']) && isset($reqParams['goods_name'])) {
                    $tmpParams = array(
                          "id" => $reqParams['goods_id'],
                          "name" => $reqParams['goods_name'],
                          "color_id" => $reqParams['color_id'],
                          "color_name" => $reqParams['color_name'],
                          "spinner_id" => $reqParams['spinner_id'],
                        ) ;
                    $objGoods = Doctrine::getTable(WacTable::$goods)->register($tmpParams, $objWeaveOrder);
                    $targetItem->setGoodsId($objGoods->getId());
                    $targetItem->setGoodsCategoryId($objGoods->getGoodsCategoryId());
                }
//                if(isset($reqParams['batch'])) {
//                    $targetItem->setBatch($reqParams['batch']);
//                }
//                if(isset($reqParams['jar_id'])) {
//                    $targetItem->setJarId($reqParams['jar_id']);
//                }
                if(isset($reqParams["color_id"]) && isset($reqParams["color_name"])) {
                    $tmpParams = array(
                            'id' => $reqParams["color_id"], 'name' => $reqParams["color_name"],
                    );
                    $objColor = Doctrine::getTable(WacTable::$color)->register($tmpParams);
                    $targetItem->setColorId($objColor->getId());
                }
                if(isset($reqParams['goods_number'])) {
                    $targetItem->setGoodsNumber($reqParams['goods_number']);
                }
                if(isset($reqParams['goods_number_unit_code'])) {
                    $targetItem->setGoodsNumberUnitCode($reqParams['goods_number_unit_code']);
                    $targetItem->setCost($reqParams['goods_number_unit_price'] * $reqParams['goods_number']);
                    $targetItem->setCurrencyCode($objWeaveOrder->getCurrencyCode());
                }
                if(isset($reqParams['goods_number_unit_price'])) {
                    $targetItem->setGoodsNumberUnitPrice($reqParams['goods_number_unit_price']);
                }
                if(isset($reqParams['processing_fee'])) {
                    $targetItem->setProcessingFee($reqParams['processing_fee']);
                }
                
                if(isset($reqParams['goods_quantity'])) {
                    $targetItem->setGoodsQuantity($reqParams['goods_quantity']);
                }
                if(isset($reqParams['reed_space'])) {
                    $targetItem->setReedSpace($reqParams['reed_space']);
                }
                if(isset($reqParams['reed_code'])) {
                    $targetItem->setReedCode($reqParams['reed_code']);
                }
                if(isset($reqParams['piece_length'])) {
                    $targetItem->setPieceLength($reqParams['piece_length']);
                }
                if(isset($reqParams['effect_requirement'])) {
                    $targetItem->setEffectRequirement($reqParams['effect_requirement']);
                }
                if(isset($reqParams['texture'])) {
                    $targetItem->setTexture($reqParams['texture']);
                }
                if(isset($reqParams['spinner_id'])) {
                    $targetItem->setSpinnerId($reqParams['spinner_id']);
                }
                if(isset($reqParams['filling_yarn_arrangement_id'])) {
                    $targetItem->setFillingYarnArrangementId($reqParams['filling_yarn_arrangement_id']);
                }
                if(isset($reqParams['warp_yarn_arrangement_id'])) {
                    $targetItem->setWarpYarnArrangementId($reqParams['warp_yarn_arrangement_id']);
                }
                if(isset($reqParams['warp_density'])) {
                    $targetItem->setWarpDensity($reqParams['warp_density']);
                }
                if(isset($reqParams['filling_density'])) {
                    $targetItem->setFillingDensity($reqParams['filling_density']);
                }

                if(isset($reqParams['memo'])) {
                    $targetItem->setMemo($reqParams['memo']);
                }

                $targetItem->save();

                // record filling yarn consume
                if(count($reqParams['filling_yarn_consume_items'])>0)
                {
                    foreach($reqParams['filling_yarn_consume_items'] as $k=>$item){
                        $goodsId = substr($k, 3);
                        $objSrcItem = Doctrine::getTable(WacTable::$weaveOrderItemCottonConsume)->findOneByOrderItemAndGoods($targetItem->getId(), $goodsId);
                        $objGoods = Doctrine::getTable(WacTable::$goods)->findOneBy("id", $goodsId);

                        $consumeAmount = $item['consume_amount'];
                        $unitConsume   = $item['unit_consume'];
                        $expectConsume = $item['expect_consume'];
                        $attritionRate = $item['attrition_rate'];
                        if($objSrcItem){
                            $consumeItem = $objSrcItem;
                        }
                        else{
                            $consumeItem = Doctrine::getTable(WacTable::$weaveOrderItemCottonConsume)->create();
                        }

                        $consumeItem->setWeaveOrderId($targetItem->getWeaveOrderId());
                        $consumeItem->setItemId($targetItem->getId());
                        $consumeItem->setCottonYarnConsumeUnitCode(Doctrine::getTable(WacTable::$systemParameter)->getValueByCode(WacAppSettingCode::$conttonUnitCode));

                        $consumeItem->setGoodsId($objGoods->getId());
                        $consumeItem->setGoodsCategoryId($objGoods->getGoodsCategoryId());

                        $consumeItem->setCottonYarnConsumeAmount($consumeAmount);
                        $consumeItem->setCottonYarnUnitConsume($unitConsume);
                        $consumeItem->setCottonYarnExpectConsume($expectConsume);
                        $consumeItem->setAttritionRate($attritionRate);

                        $consumeItem->save();
                    }
                }


                // record warp yarn consume
                if(count($reqParams['warp_yarn_consume_items'])>0)
                {
                    foreach($reqParams['warp_yarn_consume_items'] as $k=>$item){
                        $goodsId = substr($k, 3);
                        $objSrcItem = Doctrine::getTable(WacTable::$weaveOrderItemWarpYarnConsume)->findOneByOrderItemAndGoods($targetItem->getId(), $goodsId);
                        $objGoods = Doctrine::getTable(WacTable::$goods)->findOneBy("id", $goodsId);

                        $consumeAmount = $item['consume_amount'];
                        $unitConsume   = $item['unit_consume'];
                        $expectConsume = $item['expect_consume'];
                        $attritionRate = $item['attrition_rate'];
                        if($objSrcItem){
                            $consumeItem = $objSrcItem;
                        }
                        else{
                            $consumeItem = Doctrine::getTable(WacTable::$weaveOrderItemWarpYarnConsume)->create();
                        }

                        $consumeItem->setWeaveOrderId($targetItem->getWeaveOrderId());
                        $consumeItem->setItemId($targetItem->getId());
                        $consumeItem->setCottonYarnConsumeUnitCode(Doctrine::getTable(WacTable::$systemParameter)->getValueByCode(WacAppSettingCode::$conttonUnitCode));

                        $consumeItem->setGoodsId($objGoods->getId());
                        $consumeItem->setGoodsCategoryId($objGoods->getGoodsCategoryId());

                        $consumeItem->setCottonYarnConsumeAmount($consumeAmount);
                        $consumeItem->setCottonYarnUnitConsume($unitConsume);
                        $consumeItem->setCottonYarnExpectConsume($expectConsume);
                        $consumeItem->setAttritionRate($attritionRate);

                        $consumeItem->save();
                    }
                }
                
                $this->afterAdd($request, $targetItem);


            }

            $resultSet['items'][0] = $targetItem->toArray();
            $resultSet['info']     = JsCommonData::getSuccessDatum(
                    Doctrine::getTable(WacTable::$sysmsg)->getContentByCode("sys_add_succ")
            );
        }

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);

    }

    /*
   * @return list data array
    */
    public function executeEdit(sfWebRequest $request) {
        // forward to 404 if no id
        $this->forward404Unless($request->hasParameter('id'));

        if ($this->getRequest()->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $reqParams     = $this->getRequest()->getParameterHolder()->getAll();
        $resultSet     = JsCommonData::getCommonDatum();
        $inspectResult = $this->inspectDataValidation($request);

        if($inspectResult['status'] == WacOperationStatus::$Error) {
            $resultSet['info'] = $inspectResult;
        }
        else {
            $targetItem = $this->mainModuleTable->findOneById($request->getParameter('id'));

            if(count($reqParams)>0) {
                if(isset($reqParams['goods_id']) && isset($reqParams['goods_name'])) {
                    $tmpParams = array(
                          "id" => $reqParams['goods_id'],
                          "name" => $reqParams['goods_name'],
                          "color_id" => $reqParams['color_id'],
                          "color_name" => $reqParams['color_name'],
                          "spinner_id" => $reqParams['spinner_id'],
                        ) ;
                    $objGoods = Doctrine::getTable(WacTable::$goods)->register($tmpParams, $targetItem->getWacWeaveOrder());
                    $targetItem->setGoodsId($objGoods->getId());
                    $targetItem->setGoodsCategoryId($objGoods->getGoodsCategoryId());
                }
//                if(isset($reqParams['batch'])) {
//                    $targetItem->setBatch($reqParams['batch']);
//                }
//                if(isset($reqParams['jar_id'])) {
//                    $targetItem->setJarId($reqParams['jar_id']);
//                }
                if(isset($reqParams["color_id"]) && isset($reqParams["color_name"])) {
                    $tmpParams = array(
                            'id' => $reqParams["color_id"], 'name' => $reqParams["color_name"],
                    );
                    $objColor = Doctrine::getTable(WacTable::$color)->register($tmpParams);
                    $targetItem->setColorId($objColor->getId());
                }
                if(isset($reqParams['goods_number'])) {
                    $targetItem->setGoodsNumber($reqParams['goods_number']);
                }
                if(isset($reqParams['goods_number_unit_code'])) {
                    $targetItem->setGoodsNumberUnitCode($reqParams['goods_number_unit_code']);
                }
                if(isset($reqParams['goods_number_unit_price'])) {
                    $targetItem->setGoodsNumberUnitPrice($reqParams['goods_number_unit_price']);
                    $targetItem->setCost($reqParams['goods_number_unit_price'] * $reqParams['goods_number']);
                }
                if(isset($reqParams['processing_fee'])) {
                    $targetItem->setProcessingFee($reqParams['processing_fee']);
                }

                if(isset($reqParams['goods_quantity'])) {
                    $targetItem->setGoodsQuantity($reqParams['goods_quantity']);
                }
                if(isset($reqParams['reed_space'])) {
                    $targetItem->setReedSpace($reqParams['reed_space']);
                }
                if(isset($reqParams['reed_code'])) {
                    $targetItem->setReedCode($reqParams['reed_code']);
                }
                if(isset($reqParams['piece_length'])) {
                    $targetItem->setPieceLength($reqParams['piece_length']);
                }
                if(isset($reqParams['effect_requirement'])) {
                    $targetItem->setEffectRequirement($reqParams['effect_requirement']);
                }
                if(isset($reqParams['texture'])) {
                    $targetItem->setTexture($reqParams['texture']);
                }
                if(isset($reqParams['spinner_id'])) {
                    $targetItem->setSpinnerId($reqParams['spinner_id']);
                }
                if(isset($reqParams['filling_yarn_arrangement_id'])) {
                    $targetItem->setFillingYarnArrangementId($reqParams['filling_yarn_arrangement_id']);
                }
                if(isset($reqParams['warp_yarn_arrangement_id'])) {
                    $targetItem->setWarpYarnArrangementId($reqParams['warp_yarn_arrangement_id']);
                }
                if(isset($reqParams['warp_density'])) {
                    $targetItem->setWarpDensity($reqParams['warp_density']);
                }
                if(isset($reqParams['filling_density'])) {
                    $targetItem->setFillingDensity($reqParams['filling_density']);
                }

                if(isset($reqParams['memo'])) {
                    $targetItem->setMemo($reqParams['memo']);
                }

                $targetItem->save();

                // record filling yarn consume
                if(count($reqParams['filling_yarn_consume_items'])>0)
                {
                    $goodsIds = array();
                    foreach($reqParams['filling_yarn_consume_items'] as $k=>$item){
                        $goodsId = substr($k, 3);
                        $goodsIds[] = $goodsId;
                        $objSrcItem = Doctrine::getTable(WacTable::$weaveOrderItemCottonConsume)->findOneByOrderItemAndGoods($targetItem->getId(), $goodsId);
                        $objGoods = Doctrine::getTable(WacTable::$goods)->findOneBy("id", $goodsId);

                        $consumeAmount = $item['consume_amount'];
                        $unitConsume   = $item['unit_consume'];
                        $expectConsume = $item['expect_consume'];
                        $attritionRate = $item['attrition_rate'];
                        if($objSrcItem){
                            $consumeItem = $objSrcItem;
                        }
                        else{
                            $consumeItem = Doctrine::getTable(WacTable::$weaveOrderItemCottonConsume)->create();
                        }

                        $consumeItem->setWeaveOrderId($targetItem->getWeaveOrderId());
                        $consumeItem->setItemId($targetItem->getId());
                        $consumeItem->setCottonYarnConsumeUnitCode(Doctrine::getTable(WacTable::$systemParameter)->getValueByCode(WacAppSettingCode::$conttonUnitCode));

                        $consumeItem->setGoodsId($objGoods->getId());
                        $consumeItem->setGoodsCategoryId($objGoods->getGoodsCategoryId());

                        $consumeItem->setCottonYarnConsumeAmount($consumeAmount);
                        $consumeItem->setCottonYarnUnitConsume($unitConsume);
                        $consumeItem->setCottonYarnExpectConsume($expectConsume);
                        $consumeItem->setAttritionRate($attritionRate);

                        $consumeItem->save();
                    }

                    $objSubItems = $targetItem->getWacWeaveOrderItemCottonConsume();
                    if($objSubItems->count()>0) {
                        foreach($objSubItems as $objSubItem) {
                            if(!in_array($objSubItem->getGoodsId(), $goodsIds)) {
                                $objSubItem->delete();
                            }
                        }
                    }
                }


                // record warp yarn consume
                if(count($reqParams['warp_yarn_consume_items'])>0)
                {
                    $goodsIds = array();
                    foreach($reqParams['warp_yarn_consume_items'] as $k=>$item){
                        $goodsId = substr($k, 3);
                        $goodsIds[] = $goodsId;
                        $objSrcItem = Doctrine::getTable(WacTable::$weaveOrderItemWarpYarnConsume)->findOneByOrderItemAndGoods($targetItem->getId(), $goodsId);
                        $objGoods = Doctrine::getTable(WacTable::$goods)->findOneBy("id", $goodsId);

                        $consumeAmount = $item['consume_amount'];
                        $unitConsume   = $item['unit_consume'];
                        $expectConsume = $item['expect_consume'];
                        $attritionRate = $item['attrition_rate'];
                        if($objSrcItem){
                            $consumeItem = $objSrcItem;
                        }
                        else{
                            $consumeItem = Doctrine::getTable(WacTable::$weaveOrderItemWarpYarnConsume)->create();
                        }

                        $consumeItem->setWeaveOrderId($targetItem->getWeaveOrderId());
                        $consumeItem->setItemId($targetItem->getId());
                        $consumeItem->setCottonYarnConsumeUnitCode(Doctrine::getTable(WacTable::$systemParameter)->getValueByCode(WacAppSettingCode::$conttonUnitCode));

                        $consumeItem->setGoodsId($objGoods->getId());
                        $consumeItem->setGoodsCategoryId($objGoods->getGoodsCategoryId());

                        $consumeItem->setCottonYarnConsumeAmount($consumeAmount);
                        $consumeItem->setCottonYarnUnitConsume($unitConsume);
                        $consumeItem->setCottonYarnExpectConsume($expectConsume);
                        $consumeItem->setAttritionRate($attritionRate);

                        $consumeItem->save();
                    }

                    $objSubItems = $targetItem->getWacWeaveOrderItemWarpYarnConsume();
                    if($objSubItems->count()>0) {
                        foreach($objSubItems as $objSubItem) {
                            if(!in_array($objSubItem->getGoodsId(), $goodsIds)) {
                                $objSubItem->delete();
                            }
                        }
                    }
                }

                $this->afterEdit($request, $targetItem);

            }

            $resultSet['items'][0] = $targetItem->toArray();
            $resultSet['info']     = JsCommonData::getSuccessDatum(
                    Doctrine::getTable(WacTable::$sysmsg)->getContentByCode("sys_edit_succ")
            );
        }

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);

    }

    /*
     * @return list data array
     */
    public function executeGetFormData(sfWebRequest $request) {
        // forward to 404 if no id
        $this->forward404Unless($request->hasParameter('weave_order_id'));

        if ($this->getRequest()->isXmlHttpRequest()) {
            sfConfig::set('sf_web_debug', false);
        }

        $resultSet = JsCommonData::getCommonDatum();
        $resultSet['info'] = JsCommonData::getSuccessDatum();

        $objWeaveOrder = Doctrine::getTable(WacTable::$weaveOrder)->findOneBy('id', $request->getParameter('weave_order_id'));

//        $resultSet['items']['cotton_goods_category'] = $objWeaveOrder->getWacSupplier()->getStockGoodsCategoryIdNameHash();

        $resultSet['items']['unit'] = Doctrine::getTable(WacTable::$unit)->getCodeNameHash();
        $resultSet['items']['spinner'] = Doctrine::getTable(WacTable::$spinner)->getIdNameHash();
        $resultSet['items']['warp_yarn_arrangement'] = Doctrine::getTable(WacTable::$warpArrangement)->getIdNameHash();
        $resultSet['items']['filling_yarn_arrangement'] = Doctrine::getTable(WacTable::$fillingYarnArrangement)->getIdNameHash();
        $resultSet['items']['default']['goods_number_unit_code'] = Doctrine::getTable(WacTable::$systemParameter)->getValueByCode(WacAppSettingCode::$conttonUnitCode);

      $objSupplier = $objWeaveOrder->getWacSupplier();
      $limitGoodsCategorysIds = Doctrine::getTable(WacTable::$goodsCategory)->getIdsByCodes(array(WacGoodsCategory::$predefinedClassCodes["classCottonCode"]));
      $resultSet['items']['stock_filling_yarn_items'] = $objSupplier->getStockGoodsListWithDetail(array(), 1, 5, $limitGoodsCategorysIds);

      $limitGoodsCategorysIds = Doctrine::getTable(WacTable::$goodsCategory)->getIdsByCodes(array(WacGoodsCategory::$predefinedClassCodes["classDyeCode"]));
      $resultSet['items']['stock_warp_yarn_items'] = $objSupplier->getStockGoodsListWithDetail(array(), 1,5, $limitGoodsCategorysIds);

      // editing data setting
      if($request->hasParameter("id") && $request->getParameter("id")!="0")
      {
          $objItem = Doctrine::getTable(WacTable::$weaveOrderItem)->findOneBy("id", $request->getParameter("id"));
          $resultSet['items']['target_filling_yarn_items']["items"] = $objItem->getFillingYarnItemsWithDetail();
          $resultSet['items']['target_warp_yarn_items']["items"] = $objItem->getWarpYarnItemsWithDetail();
      }
      else{
          $resultSet['items']['target_filling_yarn_items'] = array();
          $resultSet['items']['target_warp_yarn_items'] = array();
      }

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }
}